import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

class SubExpandableItem extends StatefulWidget {
  final Widget header;
  final Color headerColor;
  final Color expandedBorderColor;
  final Widget expandedWidget;
  final bool isExpanded;

  const SubExpandableItem(
      {Key key,
      @required this.header,
      @required this.expandedWidget,
      this.isExpanded = false,
      this.headerColor,
      this.expandedBorderColor})
      : super(key: key);

  @override
  _SubExpandableItemState createState() => _SubExpandableItemState();
}

class _SubExpandableItemState extends State<SubExpandableItem> {
  _SubExpandableItemState();
  bool isOpen = false;

  @override
  void initState() {
    isOpen = widget.isExpanded;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return PressedOpacityButton(
      onPressed: () {
        setState(() {
          isOpen = !isOpen;
        });
      },
      child: Container(
        margin:
            EdgeInsets.symmetric(horizontal: getDp(25), vertical: getDp(13)),
        decoration: BoxDecoration(
          color: Colors.white,
          border: widget.expandedBorderColor != null
              ? Border.all(
                  color:
                      isOpen ? widget.expandedBorderColor : widget.headerColor)
              : null,
          borderRadius: BorderRadius.circular(getDp(10)),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.25),
              offset: Offset(0.0, getDp(4.0)),
              blurRadius: getDp(22),
              spreadRadius: -getDp(8),
            ),
          ],
        ),
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.symmetric(
                  horizontal: getDp(25), vertical: getDp(18)),
              decoration: BoxDecoration(
                color: widget.headerColor,
                borderRadius: BorderRadius.vertical(
                  top: Radius.circular(getDp(10)),
                  bottom: isOpen ? Radius.zero : Radius.circular(getDp(10)),
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                    child: widget.header,
                  ),
                  RotatedBox(
                    quarterTurns: isOpen ? 0 : 2,
                    child: SvgPicture.asset(
                      A.assetsImagesStyleUp,
                      width: getDp(14),
                      height: getDp(10),
                      color: aceBlackColor,
                      fit: BoxFit.contain,
                    ),
                  )
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.only(
                  left: getDp(25),
                  right: getDp(25),
                  bottom: isOpen ? getDp(18) : 0),
              child: ExpandedSection(
                expand: isOpen,
                child: widget.expandedWidget,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class ExpandedSection extends StatefulWidget {
  final Widget child;
  final bool expand;
  ExpandedSection({this.expand = false, this.child});

  @override
  _ExpandedSectionState createState() => _ExpandedSectionState();
}

class _ExpandedSectionState extends State<ExpandedSection>
    with SingleTickerProviderStateMixin {
  AnimationController expandController;
  Animation<double> animation;

  @override
  void initState() {
    super.initState();
    prepareAnimations();
    _runExpandCheck();
  }

  ///Setting up the animation
  void prepareAnimations() {
    expandController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 320));
    animation = CurvedAnimation(
      parent: expandController,
      curve: Curves.fastOutSlowIn,
    );
  }

  void _runExpandCheck() {
    if (widget.expand) {
      expandController.forward();
    } else {
      expandController.reverse();
    }
  }

  @override
  void didUpdateWidget(ExpandedSection oldWidget) {
    super.didUpdateWidget(oldWidget);
    _runExpandCheck();
  }

  @override
  void dispose() {
    expandController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizeTransition(
        axisAlignment: 1.0, sizeFactor: animation, child: widget.child);
  }
}
